package com.study.test.cglib;

import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.Method;

/**
 * @author: chenhs
 * @date: Created in 6:38 2020/4/4
 **/
public class TransactionInterceptor implements MethodInterceptor {
    @Override
    public Object intercept(Object obj, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {

        if (method.isAnnotationPresent(Transactional.class)){
            System.out.println("TransactionInterceptor：如果方法加了Transactional，则执行之前，先执行这段神奇的代码(≖ ‿ ≖)✧");
        }

        //调用被代理方法
        Object object = methodProxy.invokeSuper(obj, args);

        return object;
    }
}
